package com.core.common.manger.influx;

import com.core.common.config.InfluxValues;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
public class InfluxConfig {
    @Autowired
    private InfluxValues influxValues;


    /**
     * 创建 InfluxDBClient 客户端
     * @return
     */
    @Bean
    public InfluxDBClient influxDBClient(){
        return InfluxDBClientFactory.create(influxDBClientOptions());
    }

    private InfluxDBClientOptions influxDBClientOptions() {
        return InfluxDBClientOptions.builder()
                .url(influxValues.getUrl())
                .org(influxValues.getOrg())
                .bucket(influxValues.getBucket())
                .authenticateToken(influxValues.getToken().toCharArray())
                .build();
    }

    @Bean
    public WriteApi writeApi() {
        InfluxDBClient influxDBClient = influxDBClient();
        return influxDBClient.makeWriteApi();
    }
}

